#!/bin/bash

[ -z "${MYSQL_SQL_FILES}" ] && exit 1
PASSWORD=$1
if [ -z "${PASSWORD}" ];
then
  echo "create table need password!"
  exit 1
fi
OLD_IFS="$IFS";IFS=",";SQL_FILE_INFOS=(${MYSQL_SQL_FILES});IFS="$OLD_IFS"
for SQL_FILE_INFO in "${SQL_FILE_INFOS[@]}"
do
  #SQL_FILE_INFO[0]数据库名称或所要执行的sql文件 SQL_FILE_INFO[1]所要执行的sql文件
  OLD_IFS="$IFS";IFS=":";INFO=(${SQL_FILE_INFO});IFS="$OLD_IFS"
  [ ${#INFO[@]} -gt 1 ] && SQL_FILE=${INFO[1]} || SQL_FILE=${INFO[0]}
  if ! [ -a ${SQL_FILE} ];
  then
    echo "${SQL_FILE} not exist"
    continue
  fi
  [ ${#INFO[@]} -gt 1 ] && DATABASE="-D ${INFO[0]}" || DATABASE=""
  mysql -u root ${DATABASE} --password=${PASSWORD} --connect-expired-password < ${SQL_FILE}
done